package guet.dream.question.servlet;

import guet.dream.question.Question;
import guet.dream.question.QuestionDao;
import guet.dream.question.QuestionDaoImpl;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class QPostServlet
 */
public class QPostServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
		
		RequestDispatcher  view = request.getRequestDispatcher("/question/post.jsp");
		view.forward(request, response);
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
		String errorViewName = "/dream/question/error.jsp";
		String successViewName = "/dream/index";
		String viewName = "";
		
		String title = request.getParameter("title");
		String content = request.getParameter("content");
		
		QuestionDao questionDao = new QuestionDaoImpl();
		
		Question question = new Question(title,content);
		int count = questionDao.save(question);

		viewName = (count == 1?successViewName:errorViewName);

		response.sendRedirect(viewName);
		
	}

}
